package com.cloudRestudy.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.cloudRestudy.common.core.result.R;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author liubinyuan
 * @Title:
 * @Package
 * @Description: sentinel监控测试
 * @date 2021/12/1815:59
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/sentinel")
public class SentinelControl {
    @GetMapping("/control-a")
    public R testControl() {
        log.info("==========================controlA=========================");
        return R.ok();
    }

    @GetMapping("/control-b")
    public R testControlB() {
        log.info("==========================controlB=========================");
        return R.ok();
    }

    /**
     * 热点限流控制
     * SentinelResource  热点限流注解
     * value： 限流的key
     * blockHandler: 限流的兜底方法，当触发限流时，触发方法，可以使用默认
     * @return
     */
    @GetMapping("/hot-key")
    @SentinelResource(value = "hotKey",blockHandler = "blockHandler")
    public R testHotSentinel(@RequestParam(value = "a",required = false) String a,@RequestParam(value = "b",required = false) String b){
        log.info("=================正常=========================");
        return R.ok();
    }

    /**
     * 热点限流兜底方法
     * @param a
     * @param b
     * @param blockException 捕获移仓
     * @return
     */
    public R blockHandler(String a, String b, BlockException blockException){
        log.info("==========================热点限流============================");
        return R.ok("热点限流");
    }
}
